package com.example.teachingSystem.controller;

import com.example.teachingSystem.pojo.form.auth.*;
import com.example.teachingSystem.pojo.vo.LoginVo;
import com.example.teachingSystem.service.AuthService;
import com.example.teachingSystem.utils.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
@Tag(name = "1.认证模块")
public class AuthController {

    @Autowired
    private AuthService authService;

    @GetMapping("/captcha/login/{email}")
    @Operation(summary = "登录验证码")
    public Result<String> getLoginCaptcha(@PathVariable String email) {
        return authService.getLoginCaptcha(email);
    }

    @PostMapping("/login/pwd")
    @Operation(summary = "用户登录")
    public Result<LoginVo> userLogin(@RequestBody @Validated LoginForm loginForm) {
        return authService.userLogin(loginForm);
    }

    @PostMapping("/login/email")
    @Operation(summary = "用户邮箱登录")
    public Result<LoginVo> userLoginByEmail(@RequestBody @Validated EmailAndCaptchaForm emailAndCaptchaForm) {
        return authService.userLoginByEmail(emailAndCaptchaForm);
    }

    @PostMapping("/change/pwd")
    @Operation(summary = "通过原密码修改密码")
    public Result<String> changePwd(@RequestBody @Validated ChangePwdForm changePwdForm) {
        return authService.changeUserPassword(changePwdForm);
    }

    @GetMapping("/captcha/pwd/{email}")
    @Operation(summary = "密码修改邮箱验证码")
    public Result<String> getChangePwdCaptcha(@PathVariable String email) {
        return authService.getChangePwdCaptcha(email);
    }

    @PostMapping("/change/pwd/byEmail")
    @Operation(summary = "通过邮箱修改密码")
    public Result<String> changeUserPwdByEmail(@RequestBody @Validated ChangePwdByEmailForm changePwdByEmailForm) {
        return authService.changeUserPwdByEmail(changePwdByEmailForm);
    }

    @GetMapping("/captcha/oldEmail/{email}")
    @Operation(summary = "旧邮箱验证码", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> getOldEmailCaptcha(@PathVariable String email) {
        return authService.getOldEmailCaptcha(email);
    }

    @PostMapping("/verify/oldEmail")
    @Operation(summary = "验证旧邮箱", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> verifyOldEmail(@RequestBody @Validated EmailAndCaptchaForm emailAndCaptchaForm){
        return authService.verifyOldEmail(emailAndCaptchaForm);
    }

    @GetMapping("/captcha/newEmail/{email}")
    @Operation(summary = "新邮箱验证码", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> getNewEmailCaptcha(@PathVariable String email) {
        return authService.getNewEmailCaptcha(email);
    }

    @PostMapping("/change/newEmail")
    @Operation(summary = "验证并修改邮箱", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> verifyNewEmail(@RequestBody @Validated ChangeEmailForm changeEmailForm){
        return authService.verifyNewEmail(changeEmailForm);
    }

}
